<?php
/**
 * Created by PhpStorm.
 * User: lancer
 * Date: 2016/11/2
 * Time: 20:09
 */
header('Content-Type:text/html;charset=utf-8');
//获取注册用户信息
echo '<h2>接收到新用户注册！</h2>';
echo '<p>用户名：'.$_POST['username'].'</p>';
echo '<p>密码：'.$_POST['password'].'</p>';
echo '<p>邮箱：'.$_POST['email'].'</p>';
echo '<p>IP地址：'.$_SERVER['REMOTE_ADDR'].'</p>';
echo '<p>浏览器环境：'.$_POST['HTTP_USER_AGENT'].'</p>';
echo '<p>请求来源：'.$_POST['HTTP_REFERER'].'</p>';
//接收表单数据
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
//连接数据库，设置字符集，选择数据库
$link=mysqli_connect('localhost','root','','itacst');
if(!$link){
    die('Connect Error (' . mysqli_connect_errno() . ') '
        . mysqli_connect_error());

}
mysqli_query($link,'set names utf8');
mysqli_query($link,'use `itacst`');
//过滤用户输入数据，防止SQL注入
$username = mysqli_real_escape_string($link,$username);
$email = mysqli_real_escape_string($link,$email);
//判断用户是否已存在
$sql = "SELECT `id` FROM `user` WHERE `username`='$username'";
$rst = mysqli_query($link,$sql);
if(mysqli_fetch_row($rst)){
    die('用户名已存在，请换个用户名'
    );
}
//用户名不存在，可以注册
//使用MD5增强密码安全
$password = md5($password);
//拼接插入数据的SQL语句
$sql = "insert into `user` (`username`,`password`,`email`) VALUES 
                          ('$username','$password','$email')";
//执行SQL语句，$rst保存执行结果
$rst = mysqli_query($link,$sql);